package Dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.zelonghu.testgps.R;

import Entities.CatAdoptList;
import Entities.CatWitness;
import Entities.ClothesDonation;
import Entities.ClothesDonationList;
import SQLUtils.sqllite;

/**
 * @author zelonghu QQ:1032006226
 * @name testGPS2
 * @class name：Dialog
 * @class describe
 * @time 2019/6/28 12:44 PM
 * @change
 * @chang time
 * @class describe
 */
public class Clothes_donation_donate_Dialog extends Dialog {
    public Clothes_donation_donate_Dialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }
    public static  class Builder{
        Clothes_donation_donate_Dialog mDialog;
        EditText name_et;
        EditText telphone_et;
        EditText number_et;
        Button register;
        Button cancel;
        Double latitude;
        Double longtitude;
        String description;
        View mLayout;
        CatWitness cw;

        public Builder(final Context context , final ClothesDonation cd) {
            mDialog = new Clothes_donation_donate_Dialog(context, R.style.Theme_AppCompat_Dialog);
            LayoutInflater inflater =
                    (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //加载布局文件
            mLayout = inflater.inflate(R.layout.clothedonation_donate_dialog_layout, null, false);
            //添加布局文件到 Dialog
            mDialog.addContentView(mLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));

            name_et = (EditText)mLayout.findViewById(R.id.clothes_donation_name_tw);
            telphone_et = (EditText)mLayout.findViewById(R.id.clothes_donation_phone_tw);
            number_et = (EditText)mLayout.findViewById(R.id.clothes_donation_number_tw);
            cancel = (Button)mLayout.findViewById(R.id.clothes_donation_btn_cancel);
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mDialog.dismiss();
                }
            });
            register = (Button)mLayout.findViewById(R.id.clothes_donation_btn_submit);
            register.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //todo 首先判断其结果是否可行
                    if(isValid(context)){

                        //合理
                        ClothesDonationList cdl = new ClothesDonationList();
                        cdl.setCDDescription(cd.getCDDescription());
                        cdl.setCDdonate_name(name_et.getText().toString());
                        cdl.setCDdonate_number(Integer.parseInt(number_et.getText().toString()));
                        cdl.setCDdonate_Phone(telphone_et.getText().toString());
                        cdl.setCDID(cd.getCDID());
                        cdl.setCDLID(cd.getCDLID());
                        cdl.setCDName(cd.getCDName());
                        cdl.setCDRemark(cd.getCDRemark());
                        cdl.setCDState(cd.getCDState());
                        cdl.setCDStartTime(cd.getCDStartTime());
                        cdl.setLatitude(cd.getLatitude());
                        cdl.setLongtitude(cd.getLongtitude());
                        //添加数据库
                        sqllite.insert_Clothes_donation_List(sqllite.getDB(context),cdl);

                        //todo 访问网络并寻求返回数据

                        //todo 点击后关闭窗口

                        mDialog.dismiss();
                    }else {
                        //不合理，啥也不做
                    }

                }
            });
        }

        public Builder setmDialog(Clothes_donation_donate_Dialog mDialog) {
            this.mDialog = mDialog;
            return this;
        }

        public Builder setLatitude(Double latitude) {
            this.latitude = latitude;
            return  this;
        }

        public Builder setLongtitude(Double longtitude) {
            this.longtitude = longtitude;
            return  this;
        }

        public Clothes_donation_donate_Dialog create() {
            mDialog.setContentView(mLayout);
            mDialog.setCancelable(true);                //用户可以点击后退键关闭 Dialog
            mDialog.setCanceledOnTouchOutside(true);   //用户不可以点击外部来关闭 Dialog
            return mDialog;
        }

        /**
         * @author zelonghu
         * @time 2019/6/30  9:44 PM
         * @describe 判断用户输入的东西是否正确，会在内层指引用户
         * @param context  上下文，用于弹出TOast
         */
        private boolean isValid(Context context){
            String name = name_et.getText().toString();
            if(name.length() == 0 || name.length()>20){
                Toast.makeText(context,"请输入正确的用户名",Toast.LENGTH_LONG).show();
                return  false;

            }
            String tel = telphone_et.getText().toString();
            if(tel.length() == 0 || tel.length() > 20){
                Toast.makeText(context,"请输入正确的电话",Toast.LENGTH_LONG).show();

                return  false;
            }
            String number = number_et.getText().toString();
            if(number.length() == 0 || number.length() > 20){
                Toast.makeText(context,"请输入正确的数量",Toast.LENGTH_LONG).show();

                return  false;
            }

            return true;
        }
    }
}
